package com.crazy.crazytransactionservice.controller;

import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazycommon.exception.BusinessException;
import com.crazy.crazymodel.model.dto.subscribe.SubscribeRequest;
import com.crazy.crazymodel.model.vo.ImageVO;
import com.crazy.crazymodel.model.vo.SubscribeVO;
import com.crazy.crazytransactionservice.service.BuyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/transaction")
@Slf4j
@Api(tags = "申购相关接口")
public class BuyController {

    @Resource
    private BuyService buyService;

    @ApiOperation(value = "申购")
    @PostMapping("/subscribe/getin")
    public BaseResponse<SubscribeVO> subscribe(@RequestBody SubscribeRequest subscribeRequest){
        Integer customerID = subscribeRequest.getCustomerID();
        Integer productID = subscribeRequest.getProductID();
        if ((customerID ==null)) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"客户不存在");
        if ((productID ==null)) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"产品不存在");
        BigDecimal amount = subscribeRequest.getAmount();
        String bankCardNumber = subscribeRequest.getBankCardNumber();

        SubscribeVO subscribeVO = buyService.subscribe(customerID, productID, amount, bankCardNumber);
        return ResultUtils.success(subscribeVO);
    }

    @ApiOperation(value = "上传图片申购")
    @PostMapping("/subscribe/uploadfile")
    public BaseResponse<SubscribeVO> subscribeByImage(@RequestParam MultipartFile file){
        log.info("根据图片进行申购");
        SubscribeVO subscribeVO = buyService.subscribeByImage(file);
        return ResultUtils.success(subscribeVO);
    }

    @ApiOperation(value = "解析图片接口")
    @PostMapping("/subscribe/analyzeImage")
    public BaseResponse<ImageVO> analysis(@RequestParam MultipartFile file){
        log.info("解析图片");
        ImageVO imageVO = buyService.anaylizeImage(file);
        return ResultUtils.success(imageVO);
    }
}
